// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

#ifndef _LD_DATA_H_
#define _LD_DATA_H_

#include "bmapfile.h"
#include "options.h"

/*!
\file
\ingroup	gio
\brief		SNP Sampling Functions for LD Calculations
\details	
	This header provides methods to %sample a larger genomic data-set so that data 
	could be imported into PLINK v1.07 to perform an LD calculation centered around a particular
	SNP.
	Only use this function with larger, populous data-sets. 
	LD calculations on parse data-sets are not much use.
	SNP selection based either on a KB windows or n-mer elements upstream/downstream of the anchor SNP.
	For a 560K SNP data-set, ~1000 people that had been binary encoded by PLINK, it took PLINK 10 seconds 
	to %sample a data-set. 
	This version of the code, running on the same data was able to %sample a large data-set in less than a second.
*/

/*!
\brief Get SNP indices in MAP for LD based on KB window
\param [in] map Locus array
\param [in] nMap Size of Locus array
\param [in] snp_id String Identifier of the anchor SNP
\param [in] disp_r_window_kb Size of the KB window centered on the anchor SNP
\param [in,out] start_index Pointer to store the start index of the SNP array for LD calculation
\param [in,out] end_index Pointer to store the start index of the SNP array for LD calculation
\param [in,out] anchor_index Pointer to store the index of the anchor SNP
*/
int ld_data_get_indices(struct small_locus *map, int nMap, char *snp_id, int disp_r_window_kb, int *start_index, int *end_index, int *anchor_index);

/*! 
\brief Create a PLINK-compatible tranposed PED file
\details
	The output file name is a concatonation of \ref selected_options::rdir, \ref selected_options::szRunId
	and \ref SUFFIX_TPED_FILE.
\param [in] ops Output File location
\param [in] bpedData Array of uncompressed allelic data.
\param [in] map SNP list
\param [in] nMap Size of SNP list
\param [in] start_snp_index Start SNP to write to File
\param [in] end_snp_index End SNP to write to File
\param [in] nSamples No. samples in the data-set
*/
int ld_data_create_tpedfile(struct selected_options *ops, char *bpedData, struct small_locus *map, int nMap, int start_snp_index, int end_snp_index, int nSamples);

/*! 
\brief Create an arbitary PLINK-compatible TFAM file
\details
	The output file name is a concatonation of \ref selected_options::rdir, \ref selected_options::szRunId
	and \ref SUFFIX_TFAM_FILE.
	As LD does not require detailed PHE data, the generated TFAM file uses arbitary incremental IDs 
	for people in the data-set.
\param [in] ops Output File location
\param [in] sex Boolean array containing the gender information
\param [in] nSex Size of the gender array
*/
int ld_data_create_tfamfile(struct selected_options *ops, BOOL *sex, int nSex);

/*! 
\brief Create a simplified phenotype file for import into R-Genetics
\details
	The output file name is a concatonation of \ref selected_options::rdir, \ref selected_options::szRunId
	and \ref SUFFIX_GPHE_FILE.
	As LD does not require detailed PHE data, the generated TFAM file uses arbitary incremental IDs 
	for people in the data-set.
\param [in] ops Output File location
\param [in] sex Boolean array containing the gender information
\param [in] nSex Size of the gender array
*/
int ld_data_create_gphefile(struct selected_options *ops, BOOL *sex, int nSex);

/*!
\brief Create a simplified MAP File for R
\details
	The output file name is a concatonation of \ref selected_options::rdir, \ref selected_options::szRunId
	and \ref SUFFIX_SMAP_FILE.
	A simplified map file for data import into R in order to create a heatmap plot.
	The position of the anchor SNP is flagged.
\param [in] ops Output File location
\param [in] map SNP list
\param [in] nMap Size of SNP list
\param [in] start_snp_index Start SNP to write to File
\param [in] end_snp_index End SNP to write to File
\param [in] anchor_snp_index Index of the anchor SNP
*/
int ld_data_create_smapfile(struct selected_options *ops, struct small_locus *map, int nMap, int start_snp_index, int end_snp_index, int anchor_snp_index);

/*!
\brief Get SNP indices in MAP for LD based on element window
\param [in] map Locus array
\param [in] nMap Size of Locus array
\param [in] snp_id String Identifier of the anchor SNP
\param [in] window_element_size Size of the KB window centered on the anchor SNP
\param [in,out] start_index Pointer to store the start index of the SNP array for LD calculation
\param [in,out] end_index Pointer to store the start index of the SNP array for LD calculation
\param [in,out] anchor_index Pointer to store the index of the anchor SNP
*/
int ld_data_get_indices_nmer(struct small_locus *map, int nMap, char *snp_id, int window_element_size, int *start_index, int *end_index, int *anchor_index);

#endif